package ru.mipt.sport.gui.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import ru.mipt.sport.model.interfaces.SportLevel;
import ru.mipt.sport.model.interfaces.SportSpecialization;
import ru.mipt.sport.utils.DBUtils;

/**
 *
 * @author danon
 */
public class SportLevelsTableModel implements TableModel {

    private final DBUtils db;
    private List<SportLevel> elements;
    private SportSpecialization sport;
    
    public SportLevelsTableModel(SportSpecialization sport) {
        db = DBUtils.getInstance();
        this.sport = sport;
        refresh();
    }
    
    @Override
    public int getRowCount() {
        return elements.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public String getColumnName(int columnIndex) {
         switch(columnIndex) {
            case 0 : return "Level";
            case 1 : return "Type";
            case 2 : return "Min. value";
            case 3 : return "Max. value";
            case 4 : return "Units";
        }
        return null;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch(columnIndex) {
            case 0 : return String.class;
            case 1 : return String.class;
            case 2 : return String.class;
            case 3 : return String.class;
            case 4 : return String.class;
        }
        return Object.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex) {
            case 0 : return elements.get(rowIndex).getName();
            case 1 : return elements.get(rowIndex).getGender();
            case 2 : return elements.get(rowIndex).getMinValue();
            case 3 : return elements.get(rowIndex).getMaxValue();
            case 4 : return elements.get(rowIndex).getUnits();
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        
    }
    
    public final void refresh() {
        synchronized(db) {
            if(sport != null && sport.getId()!=null)
                elements = db.getEntityManager()
                        .createQuery("select l from SportLevelEntity l where l.sport = :sport order by l.gender, l.name, l.sport.name", SportLevel.class)
                        .setParameter("sport", sport).getResultList();
            else elements = new ArrayList<>();
        }
    }

    public void removeRows(int[] a) {
        synchronized(db) {
            try {
                EntityManager em = db.getEntityManager(true);
                for(int i : a) {
                    em.remove(elements.get(i));
                }
                db.commit();
            } catch (Exception ex) {
                db.rollback();
            }
            refresh();
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        db.close();
    }
    
}
